(function(global){
	global.TRCKR = global.TRCKR || {};

	// chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
	// 	if (tab.url.match(/chrome:\/\/history/i)) console.info(tab.url);
	// });
   	

	global.TRCKR.historyController = function(){
		var analyze = function(callback) {
			var visitsPerItem = {};

			chrome.history.search({
            	'text': '',
            	'startTime': (new Date).getTime() - 1000 * 60 * 60 * 24 * 31,
				'maxResults': 10000
        	}, function(items){
				var domain;
				for (var i=0; i<items.length; i++) {
					domain = TRCKR.getDomainFromUrl(items[i].url);
					if (!domain) {
                        continue;
                    }
					if (visitsPerItem[domain]) {
						visitsPerItem[domain]++;
					} else {
						visitsPerItem[domain] = 1;
					}
				}
				for (var x in visitsPerItem) {
					if (visitsPerItem[x] < 10) {
						delete visitsPerItem[x]
					}
				}
                TRCKR.historyView(callback(visitsPerItem)).render();
	      	});
		};

        return {
           analyze: analyze
        }
	}
})(this);